﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Office2013.Word;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.EntitiesProcess;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class People : Controller
    {
        readonly IEntityRepository<Person> _PersonService;

        public PeopleController(IEntityRepository<Person> service)
        {
            _PersonService = service;
        }
        // GET: People
        public async Task<IActionResult> Index()
        {
            var persons = await _PersonService.GetAllAsyn();

            var personVMCollection = new List<PersonViewModel>();
            var counter = 0;
            foreach (var item in persons)
            {
                var personVM = new PersonViewModel(item);
                personVM.OrderNumber = (++counter).ToString();
                personVMCollection.Add(personVM);
            }

            ViewData["Title"] = "员工数据维护管理";

            return View(personVMCollection);
        }

        // GET: People/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: People/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: People/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: People/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: People/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: People/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: People/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}
